package com.bitin.adorn.modules.system.service.impl;

import com.bitin.adorn.common.exception.BizException;
import com.bitin.adorn.modules.system.domain.Role;
import com.bitin.adorn.modules.system.domain.Employee;
import com.bitin.adorn.modules.system.repository.RoleRepository;
import com.bitin.adorn.modules.system.repository.EmployeeRepository;
import com.bitin.adorn.modules.system.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mr.Lu
 * @since 2024/5/30 15:52
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {

    private final RoleRepository roleRepository;
    private final EmployeeRepository employeeRepository;

    @Override
    public Role add(Role role) {
        return this.roleRepository.save(role);
    }

    @Override
    public List<Role> list() {
        return roleRepository.findAll();
    }

    @Override
    public Role findById(Long id) {
        return roleRepository.findById(id).orElseThrow();
    }

    @Override
    public void deleteById(Long id) {
        roleRepository.deleteById(id);
    }

    @Override
    public Role bind(Long id, Long userId) {
        Role role = roleRepository.findById(id).orElseThrow(() -> new BizException("角色不存在"));
        Employee user = employeeRepository.findById(userId).orElseThrow(() -> new BizException("用户不存在"));
        return roleRepository.save(role.bind(user));
    }
}
